package com.ly.algorithm.offerpointer;

/**
 * @Classname ListNode
 * @Description TODO
 * @Date 2020/10/4 21:17
 * @Author 冷心影翼
 */
public class ListNode {
	int val;
	ListNode next;
	ListNode() {}
	ListNode(int val) { this.val = val; }
	ListNode(int val, ListNode next) { this.val = val; this.next = next; }


	void printRes() {
		ListNode cur = this;
		while (cur!=null) {
			System.out.println(cur.val);
			cur = cur.next;
		}
	}

	static ListNode init(int[] arr) {
		if(arr.length == 0)
			return null;
		ListNode head = new ListNode(arr[0]);
		ListNode cur = head;
		for(int i=1;i<arr.length;i++) {
			cur.next = new ListNode(arr[i]);
			cur = cur.next;
		}
		return head;
	}


	//  1 2 3 4
	//-1  1
	//-1 2 1
	ListNode revese() {
		ListNode head = new ListNode(-1);
		ListNode cur = this;
		while (cur != null) {
			ListNode curNext = cur.next;
			cur.next = head.next;
			head.next = cur;
			cur = curNext;
		}
		return head.next;
	}


    //-1 1 2 3 4
	//-1 2 1 3 4
	//-1 3 2 1 4
	//-1 4 3 2 1
	ListNode revese2() {
		ListNode head = new ListNode(-1);
		head.next = this;
		ListNode cur = this;
		ListNode next = cur.next;
		while (next!=null) {
			cur.next = next.next;
			next.next = head.next;
			head.next = next;
			next = cur.next;
		}
		return head.next;
	}




	public static void main(String[] args) {
		ListNode listNode = new ListNode(1);
		listNode.next = new ListNode(2);
		listNode.next.next = new ListNode(3);
		listNode.next.next.next = new ListNode(4);
		listNode.revese().printRes();
	}


}
